package com.zhiyou.controller;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhiyou.utils.EmailUtil;
import com.zhiyou.utils.JsonResult;
@RestController
@RequestMapping("email/")
public class EmailController {

	@GetMapping("sendemail/{email}")
	public JsonResult send(@PathVariable("email")String email, HttpServletRequest request, HttpServletResponse response) {
		// 生成随机码
		String emailCode = RandomStringUtils.randomNumeric(6);// 产生一个六位的随机数
		// 生成邮箱信息
		String emailMsg = "验证码为：" + emailCode + "，请勿将验证码告知他人！";
		// 保存在session中
		request.getSession().setAttribute("CODE", emailCode);
		
		try {
			EmailUtil.sendEmail(email, emailMsg);
		
			return new JsonResult(200,"发送成功！");
		} catch (MessagingException e) {
			
			e.printStackTrace();
			return new JsonResult(500, "服务端错误，邮件发送失败，请稍后重试");
		}
	}
}
